<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - io - scene loader</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background:#000;
				color:#fff;
				padding:0;
				margin:0;
				overflow:hidden;
				font-family:georgia;
				text-align:center;
			}

			#info {
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
				font-family: Monospace;
				font-size: 13px;
				text-align: center;
				z-index:100;
			}

			#progress {
				color:red;
				top:7em;
				width: 100%;
				font-size:3em;
				font-variant:small-caps;
				font-weight:bold;
				position:absolute;
				z-index:100;
				text-align: center;
				text-shadow: #000 0px 0px 10px;
				display:none;
			}

			.shadow {
				-moz-box-shadow: 0px 0px 5px #000;
				-webkit-box-shadow: 0px 0px 5px #000;
				box-shadow: 0px 0px 5px #000;
			}

			#progressbar {
				text-align: center;
				background: white;
				width: 250px;
				height: 10px;
			}

			#bar {
				background:#d00;
				width:50px;
				height:10px;
			}

			.enabled {
				color: lime!important;
				cursor:pointer;
			}

			.enabled:hover {
				text-shadow: #0f0 0px 0px 5px !important;
			}

			.disabled {
				background:gray;
				cursor:default;
			}

			a { color:red }
			canvas { pointer-events:none; z-index:10; }

		</style>
	</head>

	<body>
		<div id="info">
			<a href="http://threejs.org">three.js</a> - scene loader test
		</div>

		<div id="progress">
			<span id="message">Loading ...</span>

			<center>
				<div id="progressbar" class="shadow"><div id="bar" class="shadow"></div></div>
			</center>
		</div>

		<script src="../build/three.js"></script>
		<script src="js/MorphAnimMesh.js"></script>
		<script src="js/loaders/collada/Animation.js"></script>
		<script src="js/loaders/collada/AnimationHandler.js"></script>
		<script src="js/loaders/collada/KeyFrameAnimation.js"></script>

		<script src="js/loaders/DDSLoader.js"></script>

		<script src="js/loaders/ctm/lzma.js"></script>
		<script src="js/loaders/ctm/ctm.js"></script>
		<script src="js/loaders/ctm/CTMLoader.js"></script>

		<script src="js/loaders/deprecated/SceneLoader.js"></script>
		<script src="js/loaders/BinaryLoader.js"></script>
		<script src="js/loaders/OBJLoader.js"></script>
		<script src="js/loaders/VTKLoader.js"></script>
		<script src="js/loaders/STLLoader.js"></script>
		<script src="js/loaders/ColladaLoader.js"></script>
		<script src="js/loaders/UTF8Loader.js"></script>
		<script src="js/loaders/MTLLoader.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var SCREEN_WIDTH = window.innerWidth;
			var SCREEN_HEIGHT = window.innerHeight;

			var container,stats;

			var camera, scene, loaded;
			var renderer;

			var mouseX = 0, mouseY = 0;

			var mixers = [];

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			var rotatingObjects = [];

			var clock = new THREE.Clock();

			document.addEventListener( 'mousemove', onDocumentMouseMove, false );

			THREE.DefaultLoadingManager.onProgress = function ( item, loaded, total ) {

				console.log( item, loaded, total );

			};

			init();
			animate();

			function $( id ) {

				return document.getElementById( id );

			}

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				var loadScene = createLoadScene();

				camera = loadScene.camera;
				scene = loadScene.scene;

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
				renderer.domElement.style.position = "relative";
				container.appendChild( renderer.domElement );

				renderer.gammaInput = true;
				renderer.gammaOutput = true;

				stats = new Stats();
				container.appendChild( stats.dom );

				var callbackProgress = function( progress, result ) {

					var bar = 250,
						total = progress.totalModels + progress.totalTextures,
						loaded = progress.loadedModels + progress.loadedTextures;

					if ( total )
						bar = Math.floor( bar * loaded / total );

					$( "bar" ).style.width = bar + "px";

				};

				var callbackFinished = function ( result ) {

					loaded = result;

					$( "message" ).style.display = "none";
					$( "progressbar" ).style.display = "none";

					result.scene.traverse( function ( object ) {

						if ( object.userData.rotating === true ) {

							rotatingObjects.push( object );

						}

						if ( object instanceof THREE.Mesh ) {

							if( object.geometry && object.geometry.animations && object.geometry.animations.length > 0 ) {

								var mixer = new THREE.AnimationMixer( object );
								mixer.clipAction( object.geometry.animations[0] ).play();
								mixers.push( mixer );

							}

						}

					} );

					//

					$( "progress" ).style.display = "none";

					camera = loaded.currentCamera;
					camera.aspect = window.innerWidth / window.innerHeight;
					camera.updateProjectionMatrix();

					scene = loaded.scene;

				};

				$( "progress" ).style.display = "block";

				THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );

				var loader = new THREE.SceneLoader();

				loader.addGeometryHandler( "binary", THREE.BinaryLoader );
				loader.addGeometryHandler( "ctm", THREE.CTMLoader );
				loader.addGeometryHandler( "vtk", THREE.VTKLoader );
				loader.addGeometryHandler( "stl", THREE.STLLoader );

				loader.addHierarchyHandler( "obj", THREE.OBJLoader );
				loader.addHierarchyHandler( "dae", THREE.ColladaLoader );
				loader.addHierarchyHandler( "utf8", THREE.UTF8Loader );

				loader.callbackProgress = callbackProgress;

				loader.load( "scenes/test_scene.js", callbackFinished );

				//

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				windowHalfX = window.innerWidth / 2;
				windowHalfY = window.innerHeight / 2;

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function onDocumentMouseMove( event ) {

				mouseX = ( event.clientX - windowHalfX );
				mouseY = ( event.clientY - windowHalfY );

			}

			function createLoadScene() {

				var result = {

					scene:  new THREE.Scene(),
					camera: new THREE.PerspectiveCamera( 65, window.innerWidth / window.innerHeight, 1, 1000 )

				};

				result.camera.position.z = 100;
				result.scene.add( result.camera );

				var object, geometry, material, light, count = 500, range = 200;

				material = new THREE.MeshLambertMaterial( { color:0xffffff } );
				geometry = new THREE.BoxGeometry( 5, 5, 5 );

				for( var i = 0; i < count; i++ ) {

					object = new THREE.Mesh( geometry, material );

					object.position.x = ( Math.random() - 0.5 ) * range;
					object.position.y = ( Math.random() - 0.5 ) * range;
					object.position.z = ( Math.random() - 0.5 ) * range;

					object.rotation.x = Math.random() * 6;
					object.rotation.y = Math.random() * 6;
					object.rotation.z = Math.random() * 6;

					object.matrixAutoUpdate = false;
					object.updateMatrix();

					result.scene.add( object );

				}

				result.scene.matrixAutoUpdate = false;

				light = new THREE.PointLight( 0xffffff );
				result.scene.add( light );

				light = new THREE.DirectionalLight( 0x111111 );
				light.position.x = 1;
				result.scene.add( light );

				return result;

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

				var delta = clock.getDelta();

				camera.position.x += ( mouseX - camera.position.x ) * .001;
				camera.position.y += ( - mouseY - camera.position.y ) * .001;

				camera.lookAt( scene.position );

				// update skinning

				THREE.AnimationHandler.update( delta * 0.75 );

				for ( var i = 0; i < rotatingObjects.length; i ++ ) {

					var object = rotatingObjects[ i ];

					if ( object.userData.rotateX ) object.rotation.x += 1 * delta;
					if ( object.userData.rotateY ) object.rotation.y += 0.5 * delta;

				}

				
				for ( var i = 0; i < mixers.length; i ++ ) {

					mixers[ i ].update( delta );

				}

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
